package com.yishun.springboot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.mapper.PermissionMapper;
import com.yishun.springboot.mapper.TbAuthoritylevelMapper;
import com.yishun.springboot.mapper.TbMenuMapper;
import com.yishun.springboot.pojo.Permissions;
import com.yishun.springboot.pojo.TbInterface;
import com.yishun.springboot.pojo.TbMenu;
import com.yishun.springboot.service.TbMenuService;
import com.yishun.springboot.util.ResultUtils;

@Service
public class TbMenuServiceImpl implements TbMenuService {

	@Autowired
	TbMenuMapper tbMenuMapper;

	@Autowired
	PermissionMapper permissionMapper;

	@Autowired
	TbAuthoritylevelMapper tbAuthoritylevelMapper;

	// 新增按钮跟按钮访问权限 还需要查询一个角色表出来 用于选择角色权限访问时添加
	@Override
	public JSONObject addTbMenu(JSONObject jsonObject) {
		String Menu_name = jsonObject.getString("menuName");// 按钮名称
		Integer pageId = jsonObject.getInteger("pageId");// 页面id 属于哪个页面的按钮
		String Path = jsonObject.getString("path");// 按钮路由
		String interfaceLocation = jsonObject.getString("interfaceLocation");// 接口地址
		String interfaceName = jsonObject.getString("interfaceName");// 接口名称
		Integer roleType = jsonObject.getInteger("roleType");// 0角色 1需要有接口权限
		String roleName = jsonObject.getString("roleName");// 权限名称 选择角色需要用到
															// 还需要改逻辑
		Integer authority_id = jsonObject.getInteger("authorityId");// 权限等级id

		TbMenu tbMenu = new TbMenu();
		tbMenu.setMenu_name(Menu_name);
		tbMenu.setPage_id(pageId);
		tbMenu.setPath(Path);
		tbMenu.setShow_or_hide(1);
		tbMenuMapper.addTbMenu(tbMenu);

		Permissions permissions = new Permissions();
		permissions.setPermission_code(interfaceLocation);
		permissions.setPermission_name(interfaceName);
		permissionMapper.addTbPermissions(permissions);
		Integer num = permissionMapper.addTbMenuPermission(tbMenu.getId(), permissions.getPermission_id());
		TbInterface tbInterface = new TbInterface();
		tbInterface.setComment(interfaceName);
		tbInterface.setInterface_name(interfaceLocation);
		if (roleType == 0 && roleName != null) {
			tbInterface.setRole_or_perms("[" + roleName + "]");
		}

		if (roleType == 1 && interfaceLocation != null) {
			tbInterface.setRole_or_perms("[" + interfaceLocation + "]");
		}
		tbAuthoritylevelMapper.addTbInterface(tbInterface);
		tbAuthoritylevelMapper.addTbAuthorityInterface(authority_id, tbInterface.getInterface_id());
		if (num >= 1) {
			return ResultUtils.jsonSuccessNotData("新增成功");
		} else {
			return ResultUtils.jsonFail("新增失败，请联系管理员");
		}
		// 新增按钮跟接口名称 地址之类的 自动获取两个表的新增id 再新增进中间表里
	}

	@Override
	public JSONObject delTbMenu(JSONObject jsonObject) {
		Integer menuId = jsonObject.getInteger("menuId");
		Integer id = tbMenuMapper.getTbMenuPermission(menuId);
		tbMenuMapper.delMenu(menuId);
		Integer num = permissionMapper.delTbPermissions(id);
		if (num >= 1) {
			return ResultUtils.jsonSuccessNotData("删除成功");
		} else {
			return ResultUtils.jsonFail("删除失败，请联系管理员");
		}
	}

}
